package com.japstudy.controller;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.japstudy.entity.User;
import com.japstudy.service.IUserService;
import com.japstudy.util.UUIDUtil;

@Controller
@RequestMapping("/User")
public class UserController {
	
	@Autowired
	private IUserService userServiceImpl;
	
	@RequestMapping("/indexUi")
	public String indexUi(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "user/index";
	}
	
	@RequestMapping("/registUi")
	public String registUi(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "user/regist";
	}
	
	@RequestMapping("/loginUi")
	public String loginUi(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Cookie[] cookies = request.getCookies();
		if (cookies != null && cookies.length > 0) {
			for (Cookie cookie : cookies) {
				String name = cookie.getName();
				String value = cookie.getValue();
				if (name.equals("loginUserId")) {
					User loginUser = userServiceImpl.selectById(value);
					if (loginUser != null) {
						request.getSession().setAttribute("loginUser", loginUser);
						response.sendRedirect(request.getContextPath() + "/User/indexUi");
						return null;
					}
				}
			}
		}
		return "user/login";
	}
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request, HttpServletResponse response, User user) throws IOException {
		User loginUser = userServiceImpl.login(user);
		String rememberMe = request.getParameter("rememberMe");
		if(loginUser != null) {
			request.getSession().setAttribute("loginUser", loginUser);
			if (rememberMe != null) {
				Cookie cookie = new Cookie("loginUserId", loginUser.getUserId());
				cookie.setPath(request.getContextPath());
				cookie.setMaxAge(60 * 60 * 24 * 7);
				response.addCookie(cookie);
			}
			response.sendRedirect(request.getContextPath() + "/User/indexUi");
			return null;
		} else {
			response.sendRedirect(request.getContextPath() + "/User/loginUi");
			return null;
		}
	}
	
	@RequestMapping("/haveSameAccount")
	public @ResponseBody String haveSameAccount(User user) throws IOException {
		String userAccount = user.getUserAccount();
		User haveUser = userServiceImpl.selectByAccount(userAccount);
		if(haveUser == null) {
			return "no";
		} else {
			return "yes";
		}
	}
	
	@RequestMapping("/regist")
	public String regist(HttpServletRequest request, HttpServletResponse response, User user) throws IOException {
		user.setUserId(UUIDUtil.getUUID());
		user.setAdminLogin("00");
		int result = userServiceImpl.regist(user);
		if(result == 1) {
			request.getSession().setAttribute("loginUser", user);
			response.sendRedirect(request.getContextPath() + "/User/indexUi");
			return null;
		} else {
			response.sendRedirect(request.getContextPath() + "/User/registUi");
			return null;
		}
	}
	
	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Object loginUser = request.getSession().getAttribute("loginUser");
		if(loginUser != null) {
			request.getSession().removeAttribute("loginUser");
		}
		Cookie cookie = new Cookie("loginUserId", "-1");
		cookie.setPath(request.getContextPath());
		cookie.setMaxAge(0);
		response.addCookie(cookie);
		response.sendRedirect(request.getContextPath() + "/User/loginUi");
		return null;
	}
}
